Source code for

# ******************************************************************************
# Copyright 2017-2018 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************

import socketserver
import argparse
import pickle
import logging
import sys
import re
from import set_expand
from import validate_existing_filepath, check_size
from import load_parser, extract_noun_phrases

logger = logging.getLogger(__name__)

[docs]class MyTCPHandler(socketserver.BaseRequestHandler): """ A simple server to load the w2v model and handle expand requests from the ui """
[docs] def handle(self):"handling expand request") res = '' = pickle.loads(self.request.recv(10240))'request data: %s', req =[0] if req == 'get_vocab':'getting vocabulary') res = se.get_vocab() elif req == 'in_vocab': term =[1] res = se.in_vocab(term) elif req == 'get_group': term =[1] res = se.get_group(term) elif req == 'annotate': seed =[1] text =[2] res = self.annotate(text, seed)"res:%s", str(res)) elif req == 'expand':'expanding') data = [x.strip() for x in[1].split(',')] res = se.expand(data)'compressing response') packet = pickle.dumps(res)'response length= %s', str(len(packet)))'sending response') self.request.sendall(packet)'done')
[docs] @staticmethod def annotate(text, seed): # remove extra spaces from text text = re.sub(r'\s\s+', " ", text) np_list = [] docs = [text] spans = extract_noun_phrases(docs, nlp, args.chunker) for x in spans: np = x.text if np not in np_list: np_list.append(np)"np_list=%s", str(np_list)) return se.similarity(np_list, seed, args.similarity)
if __name__ == "__main__": parser = argparse.ArgumentParser(prog='') parser.add_argument('model_path', metavar='model_path', type=validate_existing_filepath, help='a path to the w2v model file') parser.add_argument('--host', type=str, default='localhost', help='set port for the server', action=check_size(1, 20)) parser.add_argument('--port', type=int, default=1234, help='set port for the server', action=check_size(0, 65535)) parser.add_argument('--grouping', action='store_true', default=False, help='grouping mode') parser.add_argument('--similarity', default=0.5, type=float, action=check_size(0, 1), help='similarity threshold') parser.add_argument('--chunker', type=str, choices=['spacy', 'nlp_arch'], help='spacy chunker or \'nlp_arch\' for NLP Architect NP Extractor') args = parser.parse_args() port = args.port model_path = args.model_path"loading model") se = set_expand.SetExpand(model_path, grouping=args.grouping)"loading chunker") nlp = load_parser(args.chunker)"loading server") HOST, PORT =, port server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)"server loaded") server.serve_forever()